package ar.com.globallogic.promomr.domain;

import java.util.Date;

import ar.com.globallogic.promomr.exceptions.InvalidPublicationStateTransitionException;

public class PendingState extends PublicationState {

	private Date publicationDate;
	
	public PendingState(Date publicationDate) {
		super();
		this.publicationDate = publicationDate;
	}

	@Override
	public String getName() {
		return "PENDING";
	}

	@Override
	public void publish(Publication publication) {
		if(dayHasArrived()){
			publication.setState(new PublishedState());
		}else{
			throw new InvalidPublicationStateTransitionException(publication.getId(), this.getName());
		}
	}

	@Override
	public void publishAt(Publication publication, Date publicationDate) {
		this.publicationDate = publicationDate;
	}

	@Override
	public void cancel(Publication publication) {
		publication.setState(new UploadedState());
	}
	
	private boolean dayHasArrived() {
		Date today = new Date();
		return publicationDate.before(today) || publicationDate.equals(today);
	}
	
	public Date getPublicationDate(){
		return publicationDate;
	}
}
